package com.quanxiaoha.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.quanxiaoha.weblog.common.domain.dos.UserDO;
import org.apache.catalina.User;

import java.time.LocalDateTime;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2023-08-22 17:06
 * @description: TODO
 **/
public interface UserMapper extends BaseMapper<UserDO> {

    /**
     * 根据用户名查找用户
     *
     * @param username
     * @return
     */
    default UserDO findByUsername(String username) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getUsername, username);
        return selectOne(wrapper);
    }


    default int updatePasswordByUsername(String username, String encodePassword) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        //封装更新内容
        UserDO userDO = UserDO.builder().password(encodePassword).updateTime(LocalDateTime.now()).build();

        //构造更新条件
        wrapper.eq(UserDO::getUsername, username);

        //传入条件进行更新,返回受影响行数
        return update(userDO, wrapper);
    }
}
